﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

/*
 * Player class holds information for the player and their various functions.
 * Includes positions textures, game play related to the player stuff.
 */

namespace LastStand.gameContent
{
	public class Player : Sprite
	{
		//what day is the player on?
		private int dayLevel;

		public int DayLevel
		{
			get { return this.dayLevel; }
			set { this.dayLevel = value; }
		}

		//the vector position of the player's crosshair
		//when they attack.
		private Vector2 crosshairPos;

		public Vector2 GetCrosshairPos
		{
			get { return this.crosshairPos; }
		}

		private Vector2 gunPos;

		public Vector2 GetGunPos
		{
			get { return this.gunPos; }
		}

		//the players crosshair texture
		private Texture2D crossTex;

		//a touch collection to hold the players touches
		TouchCollection touches;

		//the timer for the player's shooting speed
		private float shotTimer;
		
		//the interval the player will wait between taking shots.
		private float shootInterval;

		//if the player's shot timer has reached 0, they can shoot.
		private bool canAttack;

		//the damage of the player's weapon.
		private float weaponDamage;

		public float GetWeaponDamage
		{
			get { return weaponDamage; }
		}

		public Player(Vector2 spawn)
		{
			//sets the texture file name for the player
			this.TexFileName = "playerBase";

			//create crosshair in center of screen
			this.crosshairPos = new Vector2(SharedGraphicsDeviceManager.DefaultBackBufferWidth / 2,
								SharedGraphicsDeviceManager.DefaultBackBufferHeight / 2);

			//TODO: Incorporate into save (if any)
			this.shotTimer = 0.0f;
			this.shootInterval = 3000.0f;
			this.weaponDamage = 5.0f;

			//player can attack by default
			this.canAttack = true;
		}

		//draws the player object textures at their position on the screen
		public override void Draw(SpriteBatch sb)
		{
			sb.Begin();
				sb.Draw(this.Texture,
					this.GetPosition,
					null,
					Microsoft.Xna.Framework.Color.White,
					0.0f,
					Vector2.Zero, 
					1.0f,
					SpriteEffects.None,
					0);

				sb.Draw(this.crossTex,
					this.crosshairPos,
					null,
					Microsoft.Xna.Framework.Color.White,
					0.0f,
					new Vector2((this.crossTex.Width / 2), (this.crossTex.Height)),
					4.0f,//increase size of crosshair on screen so player can see
					SpriteEffects.None,
					0);

			sb.End();
		}

		public override void Update(GameTimerEventArgs time)
		{
			//get touches
			this.pollUser();

			//if they cant attack...
			if (!this.canAttack)
			{
				//decrement the shot timer...
				this.shotTimer -= time.ElapsedTime.Milliseconds;

				//...until they can.
				if (this.shotTimer <= 0)
				{
					this.canAttack = true;
				}
			}
		}
	

		//when the player releases their finger from the screen
		//the crosshair appears at vector2 dest
		protected void attack(Vector2 dest)
		{
			//reset player to not being able to attack
			this.canAttack = false;

			//Shot shot = new Shot(dest);

			//reset shot timer.
			this.shotTimer = this.shootInterval;
		}

		//get user information for touches
		protected void pollUser()
		{
			//get all touches and add to touchcollection
			this.touches = TouchPanel.GetState();

			//if user isnt touching anything
			if (this.touches.Count > 0)
			{
				//grab first touch
				TouchLocation first = touches[0];

				//assign to crosshair position
				this.crosshairPos = first.Position;

				// !!!FIX  if (gameContent.GameCore.GameState == PAUSED)
				{

				}
			}

			//if player can shoot and they have "just released"
			//the crosshair (not touching screen anymore),
			//then fire the gun.
			if (this.canAttack & this.touches.Count == 0)
			{
				//sends in crosshair position to attack dest
				this.attack(crosshairPos);
			}
		}
	}
}
